package app.android.alampang.ui.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.LinkedList;
import java.util.List;

import app.android.alampang.R;
import app.android.alampang.model.Note;
import app.android.alampang.ui.adapter.NotesAdapter;
import app.android.alampang.ui.core.BaseActivity;
import app.android.alampang.ui.custom.FloatingActionButton;
import app.android.alampang.ui.custom.QuiltView;
import app.android.alampang.utility.DatabaseHandler;
import app.android.alampang.utility.NavigationService;

/**
 * Created by Lihong on 13/02/2015.
 */
public class NotesActivity extends BaseActivity {

    private QuiltView quiltView;
    private FloatingActionButton fab;
    private List<Note> notes;

    @Override
    public void onCreate(Bundle savedInstanceState, Bundle param) {
        setContentView(R.layout.activity_notes);
    }

    @Override
    public void initialize() {
        fab = (FloatingActionButton) findViewById(R.id.btn_new);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavigationService
                        .getInstance()
                        .navigate(NotesActivity.this, CreateNoteActivity.class, true);
            }
        });


        quiltView = (QuiltView) findViewById(R.id.quilt);
        quiltView.setChildPadding(0);

        DatabaseHandler db = new DatabaseHandler(getApplicationContext());
        notes = db.getAllNotes();


        for (Note n : notes) {

            ImageView image = new ImageView(this.getApplicationContext());
            image.setScaleType(ImageView.ScaleType.CENTER_CROP);

            Bitmap bmp = BitmapFactory.decodeFile(n.getImageUri());
            image.setImageBitmap(bmp);
            image.setOnClickListener(new OnImageClickListener(this, n));

            quiltView.addPatchImage(image);
        }
    }

    @Override
    public void onLock() {

    }

    @Override
    public void onUnLock() {

    }

    @Override
    public void onReset() {

    }

    class OnImageClickListener implements View.OnClickListener
    {
        private Context context;
        private Note note;

        public OnImageClickListener(Context context, Note note)
        {
            this.note = note;
            this.context = context;
        }

        @Override
        public void onClick(View v) {
            Bundle param = new Bundle();
            param.putString(Note.PARSING_NAME , note.toJSONObject().toString());
            NavigationService.getInstance()
                    .navigate(context, CreateNoteActivity.class, param, true);
            //Toast.makeText(context, note.getName() + " ", Toast.LENGTH_LONG).show();
        }
    }
}
